﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace Zero.UControl.Component
{
    /// <summary>
    /// Fan.xaml 的交互逻辑
    /// </summary>
    public partial class Fan : UserControl
    {
        private RotateTransform rotateTrans = new RotateTransform(); 
        private DoubleAnimation angleAnimation = new DoubleAnimation();  
        private Storyboard storyboard = new Storyboard(); 

        public Fan()
        {
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            rotateTrans.CenterX = img_FanRotate.Width / 2;
            rotateTrans.CenterY = img_FanRotate.Height / 2;
            img_FanRotate.RenderTransform = rotateTrans;

            angleAnimation.Duration = new Duration(TimeSpan.FromMilliseconds(600));
            angleAnimation.From = 0;
            angleAnimation.To = 360;

            // 绑定动画至目标属性
            DependencyProperty[] propertyChain = new DependencyProperty[]
            {
                RenderTransformProperty,
                RotateTransform.AngleProperty
            };

            Storyboard.SetTargetName(angleAnimation, img_FanRotate.Name);
            Storyboard.SetTargetProperty(angleAnimation, new PropertyPath("(0).(1)", propertyChain));
            storyboard.Children.Add(angleAnimation);

            // 设置故事板行为
            storyboard.Duration = new Duration(TimeSpan.FromMilliseconds(600));
            storyboard.RepeatBehavior = RepeatBehavior.Forever;
        }


        public FanState RunState
        {
            get { return (FanState)GetValue(RunStateProperty); }
            set { SetValue(RunStateProperty, value); }
        }

        public static readonly DependencyProperty RunStateProperty =
            DependencyProperty.Register("RunState", typeof(FanState), typeof(Fan), new PropertyMetadata(FanState.STOP, OnFanStateChange));


        public double Speed
        {
            get { return (double)GetValue(SpeedProperty); }
            set { SetValue(SpeedProperty, value); }
        }

        public static readonly DependencyProperty SpeedProperty =
            DependencyProperty.Register("Speed", typeof(double), typeof(Fan), new PropertyMetadata(0d, OnSpeedChange));

        private void ApplyFanState()
        {
            switch (RunState)
            {
                case FanState.RUN:
                    storyboard.Begin(img_FanRotate, true);
                    break;
                case FanState.PAUSE:
                    storyboard.Pause(img_FanRotate);
                    break;
                case FanState.RESUME:
                    storyboard.Resume(img_FanRotate);
                    break;
                case FanState.STOP:
                    storyboard.Stop(img_FanRotate);
                    break;
                default:
                    break;
            }
        }

        private void ApplyFanSpeed()
        {
            storyboard.SetSpeedRatio(img_FanRotate, Speed / 2000);
        }

        private static void OnFanStateChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(d is Fan fan)
            {
                fan.ApplyFanState();
            }   
        }

        private static void OnSpeedChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if(d is Fan fan)
            {
                fan.ApplyFanSpeed();
            }
        }
    }
    public enum FanState
    {
        RUN, PAUSE, RESUME, STOP
    }
}
